package com.tanhua.server.controller;


import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RequestMapping("/users")
@RestController
public class UsersController {


    @Autowired
    private UsersService usersService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /*
     *  互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     *    请求方式和请求链接 : GET  /friends/{type}
     *    请求参数:  type
     *            1 互相关注
     *              2 我关注
     *              3 粉丝
     *              4 谁看过我
     *             Query : page,pagesize,nickname(暂时不需要)
     *    返回值:  PageResult<UserLikeListVo>
     * */

  /*  @GetMapping("/friends/{type}")
    public ResponseEntity friends(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  @PathVariable("type") Integer type) {


        PageResult result = usersService.friends(page, pagesize, type);

        return ResponseEntity.ok(result);
    }*/


    /*
     * 用户资料 - 读取
     * 请求方法: GET
     * 请求参数: Authorization token ,Query userID(用户id，当不传递时，查询当前用户的资料信息)
     * 返回值 :  ResponseEntity  -->UserInfo
     * */
    @GetMapping
    public ResponseEntity users(Long userID) {

        Long id = UserHolder.getUserId();

        if (id == null) {
            id = userID;
        }

        UserInfoVo userInfoVo = usersService.findById(id);

        return ResponseEntity.ok(userInfoVo);
    }

    /*
     * 请求方式: PUT
     * 请求参数: Authorization token ,UserInfo
     * 返回值:无  ResponseEntity
     *
     * */

    @PutMapping
    public ResponseEntity upload(@RequestBody UserInfo userInfo) {

        Long id = UserHolder.getUserId();

        userInfo.setId(id);

        usersService.updateUserInfo(userInfo);

        return ResponseEntity.ok(null);
    }

    /*
     *  用户通用设置 - 读取
     *   请求方式和请求链接: GET  /settings
     *   请求参数: Authorization token
     *   返回值: ResponseEntity  封装好的SettingsVo对象
     * */
    @GetMapping("/settings")
    public ResponseEntity settings() {

        //1.获取用户id
        Long userId = UserHolder.getUserId();
        String mobile = UserHolder.getMobile();

        //调用usersService.settings方法 返回SettingsVo对象
        SettingsVo vo = usersService.settings(userId, mobile);

        return ResponseEntity.ok(vo);
    }


    /*
     * 设置陌生人问题 - 保存
     *  请求方式和请求链接 : POST  /questions
     *  请求参数: Authorization token
     *           Body content -->map
     *  返回值:ResponseEntity 无
     * */

    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {

        String content = (String) map.get("content");

        usersService.saveQuestion(content);

        return ResponseEntity.ok(null);

    }


    /*
     *通知设置 - 保存
     *  请求方式和请求链接:  POST  /notifications/setting
     *   请求参数 :Authorization token
     *           map :likeNotification,pinglunNotification,gonggaoNotification
     * 返回值 : ResponseEntity 无
     *
     * */
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map) {

        usersService.setting(map);
        return ResponseEntity.ok(null);
    }

    /*
     * 黑名单 - 翻页列表
     * 请求方式和请求链接:   GET  /blacklist
     * 请求参数 :Authorization token
     *       query: page ,pagesize
     * 返回值 : ResponseEntity  封装的对象 PageResult
     * */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "2") Integer pagesize) {
        PageResult result = usersService.blacklist(page, pagesize);

        return ResponseEntity.ok(result);
    }

    /*
     * 黑名单 - 移除
     *
     *   请求方式和请求链接:   DELETE  /blacklist/:uid
     *   请求参数 : uid 用户id(black_user_id)
     *   返回值 : ResponseEntity 无
     * */

    @DeleteMapping("/blacklist/{blackUserId}")
    public ResponseEntity deleteBlackUser(@PathVariable("blackUserId") Long blackUserId) {
        usersService.deleteBlackUser(blackUserId);

        return ResponseEntity.ok(null);
    }


    /*
     * 修改手机号- 1 发送短信验证码
     * 请求方式和请求链接  POST /phone/sendVerificationCode
     * 请求参数  Authorization
     * 返回值 无
     * */

    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {

        String verificationCode = "888888";

        redisTemplate.opsForValue().set("verificationCode", verificationCode);

        return ResponseEntity.ok(null);
    }


    /*
     *  修改手机号 - 2 校验验证码
     * 请求方式和请求链接  POST  /phone/checkVerificationCode
     * 请求参数:Body verificationCode   文本 map
     * 返回值:  verification boolean
     * */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {

        String verificationCode = (String) map.get("verificationCode");
        Boolean verification = usersService.checkVerificationCode(verificationCode);

        Map respMap = new HashMap();
        respMap.put("verification", verification);
        return ResponseEntity.ok(respMap);
    }

    /*
     * 修改手机号 - 3 保存
     *  请求方式和请求链接 POST  /phone
     *  请求参数:  body phone
     *  返回值: 无
     * */

    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map) {

        String phone = (String) map.get("phone");
        usersService.updateMobile(phone);

        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计 :
     * 请求链接 : GET/users/counts
     * 返回值 : countsVo
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        CountsVo vo = usersService.counts();
        return ResponseEntity.ok(vo);
    }

    /*
     *  互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     *    请求方式和请求链接 : GET  /friends/{type}
     *    请求参数:  type
     *            1 互相关注
     *              2 我关注
     *              3 粉丝
     *              4 谁看过我
     *             Query : page,pagesize,nickname(暂时不需要)
     *    返回值:  PageResult<UserLikeListVo>
     * */
    //todo
    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  @PathVariable("type") Integer type) {


        PageResult result = usersService.friends(page, pagesize, type);

        return ResponseEntity.ok(result);
    }
    /**
     * 粉丝 - 喜欢 :
     *    请求连接 : POST/users/fans/:uid
     *    请求参数 : uid
     *
     */
     @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable("uid") Long userLikeId){
         usersService.fans(userLikeId);
         return ResponseEntity.ok(null);
     }

    /**
     * 喜欢 - 取消:
     *    请求连接 : DELETE/users/like/:uid
     *    请求参数 : uid
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity deleteLike(@PathVariable("uid") Long likeUserId){
        usersService.deleteLike(likeUserId);
        return ResponseEntity.ok(null);
    }
}
